﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Mono.Cecil;
using Ue.CQRS.Utility;

namespace Ue.CQRS.Tests.Utility
{
    [TestClass]
    public class MonoExtensionsTests
    {
        [TestMethod]
        public void GetTypes()
        {
            var types = this.GetAssembly()
                .Modules
                .SelectMany(module => module.GetTypes(typeof(FakeMonoAttribute)));

            Assert.AreEqual(1, types.Count());
            Assert.AreEqual("FakeMonoObject", types.FirstOrDefault().Name);
        }

        [TestMethod]
        public void GetMethods()
        {
            var methods = this.GetAssembly()
                .Modules
                .SelectMany(module => module.Types)
                .SelectMany(t => t.GetMethods(typeof(FakeMonoAttribute)));

            Assert.AreEqual(2, methods.Count());
            Assert.AreEqual("Method1", methods.FirstOrDefault().Name);
        }

        public AssemblyDefinition GetAssembly()
        {
            var path = string.Format("{0}\\{1}.dll",AppDomain.CurrentDomain.BaseDirectory,System.Reflection.Assembly.GetExecutingAssembly().FullName.Split(',')[0]);

            return AssemblyDefinition.ReadAssembly(path);
        }
    }

    public class FakeMonoAttribute : Attribute
    {
    }

    [FakeMono]
    public class FakeMonoObject
    {
        [FakeMono]
        public void Method1()
        {
        }

        [FakeMono]
        public void Method2()
        {
        }

        public void Method3()
        {
        }
    }
}
